package com.chuangketiebeta;

import com.chuangketiebeta.ui.CartActivity;
import com.chuangketiebeta.ui.CategoryActivity;
import com.chuangketiebeta.ui.IndexActivity;
import com.chuangketiebeta.ui.PersonalActivity;
import com.chuangketiebeta.ui.SearchActivity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

public class MainActivity extends TabActivity {
	private RadioGroup mRadioGroup ;
	private TabHost mTabHost ;
	public static final String TAB_MAIN = "MAIN_ACTIVITY";
	public static final String TAB_SEARCH = "SEARCH_ACTIVITY";
	public static final String TAB_CATEGORY = "CATEGORY_ACTIVITY";
	public static final String TAB_CART = "CART_ACTIVITY";
	public static final String TAB_PERSONAL = "PERSONAL_ACTIVITY";

	protected void onCreate(Bundle savedInstanceState) {
		AppManager.getInstance().addActivity(MainActivity.this);
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		findViewById();
		initView();
	}

	protected void findViewById() {
		// TODO Auto-generated method stub
		mRadioGroup = (RadioGroup) findViewById(R.id.home_radio_button_group);
		
	}

	protected void initView() {
		// TODO Auto-generated method stub
		mTabHost = getTabHost();

		Intent i_main = new Intent(this, IndexActivity.class);
		Intent i_search = new Intent(this, SearchActivity.class);
		Intent i_category = new Intent(this, CategoryActivity.class);
		Intent i_cart = new Intent(this, CartActivity.class);
		Intent i_personal = new Intent(this, PersonalActivity.class);

		mTabHost.addTab(mTabHost.newTabSpec(TAB_MAIN).setIndicator(TAB_MAIN)
				.setContent(i_main));
		mTabHost.addTab(mTabHost.newTabSpec(TAB_SEARCH)
				.setIndicator(TAB_SEARCH).setContent(i_search));
		mTabHost.addTab(mTabHost.newTabSpec(TAB_CATEGORY)
				.setIndicator(TAB_CATEGORY).setContent(i_category));
		mTabHost.addTab(mTabHost.newTabSpec(TAB_CART).setIndicator(TAB_CART)
				.setContent(i_cart));
		mTabHost.addTab(mTabHost.newTabSpec(TAB_PERSONAL)
				.setIndicator(TAB_PERSONAL).setContent(i_personal));

		mTabHost.setCurrentTabByTag(TAB_MAIN);
		
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.home_tab_main:
					mTabHost.setCurrentTabByTag(TAB_MAIN);
					break;

				case R.id.home_tab_search:
					mTabHost.setCurrentTabByTag(TAB_SEARCH);
					break;

				case R.id.home_tab_category:
					mTabHost.setCurrentTabByTag(TAB_CATEGORY);
					break;

				case R.id.home_tab_cart:
					mTabHost.setCurrentTabByTag(TAB_CART);
					break;

				case R.id.home_tab_personal:
					mTabHost.setCurrentTabByTag(TAB_PERSONAL);
					break;
				}
				
			}
		});
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
